//
//  pictureDetailController.m
//  YouGoWeGo
//
//  Created by 范业君 on 14-11-26.
//  Copyright (c) 2014年 LiuFei. All rights reserved.
//

#import "pictureDetailController.h"
#import "UIImageView+WebCache.h"
#import "picDetailModel.h"
#import "commentModel.h"
#import "commentCell.h"


@interface pictureDetailController ()<UITableViewDataSource, UITableViewDelegate>

@end

@implementation pictureDetailController


- (void)dealloc
{
    [_aView release];
    [_infor release];
    [_headImage release];
    [_name release];
    [_trip_ads release];
    [_backImage release];
    [_time release];
    [_smallImage release];
    [_address release];
    [_like release];
    [_likeCount release];
    [_comment release];
    [_commentCount release];
    [_scroller release];
    [_model release];
    [_arr release];
    [_tableView release];
    [_array release];
    [_one release];
    [_two release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.array = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view setBackgroundColor:[UIColor redColor]];
    [self createBackScrollerView];
    [self createViews];
    
    [self startAsync];
    [self createaView];
    
   
}

- (void)createTableView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, _comment.frame.origin.y + _comment.frame.size.height + 10, self.view.frame.size.width, [_array count] * 50) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    [_tableView setRowHeight:50];
    [_tableView setSeparatorStyle: UITableViewCellSeparatorStyleNone];
    [_tableView registerClass:[commentCell class] forCellReuseIdentifier:@"ok"];
    [_scroller addSubview:_tableView];
    [_tableView release];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_array count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    commentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ok"];
    commentModel *cModel = [_array objectAtIndex:indexPath.row];
    cell.comModel = cModel;
    [cell setBackgroundColor:[UIColor colorWithRed:228 / 255.0 green:228 / 255.0  blue:228 / 255.0  alpha:1]];
    return cell;
}



- (void)createBackScrollerView
{
    self.scroller = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.scroller setBackgroundColor:[UIColor colorWithRed:228 / 255.0 green:228 / 255.0  blue:228 / 255.0  alpha:1]];
    [self.view addSubview:_scroller];
    [_scroller setContentSize:CGSizeMake(0,_tableView.frame.size.height + _tableView.frame.origin.y + self.view.frame.size.height)];
    [_scroller release];
}

- (void)createaView
{
    self.aView = [[UIView alloc] initWithFrame:CGRectZero];
    [_aView setBackgroundColor:[UIColor whiteColor]];
    
   
    ;
    [_aView  addSubview:_backImage];
    [_aView  addSubview:_infor];
    [_aView  addSubview:_time];
    [_aView  addSubview:_smallImage];
    [_aView  addSubview:_address];
    //[_aView  addSubview:_like];
    [self.scroller addSubview:_aView];
    [_aView release];
   
}

- (void)createViews
{
    self.arr = [NSMutableArray array];
    self.headImage = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 45, 45)];
   // [_headImage setBackgroundColor:[UIColor redColor]];
    [_headImage.layer setCornerRadius:22.5];
    [_headImage setClipsToBounds:YES];
    [_scroller addSubview:_headImage];
    [_headImage release];
    
    
    self.name = [[UILabel alloc] initWithFrame:CGRectMake(_headImage.frame.origin.x + _headImage.frame.size.width + 10, _headImage.frame.origin.y, 100, 25)];
    [_name setFont:[UIFont systemFontOfSize:12]];
    //[_name setBackgroundColor:[UIColor redColor]];
    [_scroller addSubview:_name];
    [_name release];
    
    
    self.trip_ads = [[UILabel alloc] initWithFrame:CGRectMake(_name.frame.origin.x, _name.frame.origin.y + _name.frame.size.height + 5, 150, 25)];
    [_trip_ads setFont:[UIFont systemFontOfSize:12]];
    //[_trip_ads setBackgroundColor:[UIColor redColor]];
    [_scroller addSubview:_trip_ads];
    [_trip_ads release];
    
    self.backImage = [[UIImageView alloc] initWithFrame:CGRectZero];
    [_address setFont:[UIFont systemFontOfSize:12]];
    [_backImage release];

    self.infor = [[UILabel alloc] initWithFrame:CGRectZero];
    [_address setFont:[UIFont systemFontOfSize:12]];
    [_infor setFont:[UIFont systemFontOfSize:12]];
    [_infor setNumberOfLines:0];
    //[_infor setBackgroundColor:[UIColor redColor]];
    [_infor release];
    
    
    self.time = [[UILabel alloc] initWithFrame:CGRectZero];
    [_address setFont:[UIFont systemFontOfSize:12]];
    [_time setFont:[UIFont systemFontOfSize:12]];
    [_time release];
    
    self.smallImage = [[UIImageView alloc] initWithFrame:CGRectZero];
    [_address setFont:[UIFont systemFontOfSize:12]];
    [_smallImage release];
    
    
    self.address = [[UILabel alloc] initWithFrame:CGRectZero];
    [_address setFont:[UIFont systemFontOfSize:12]];
    [_address release];
    
    self.like = [[UILabel alloc] initWithFrame:CGRectZero];
    [_like setText:@"喜欢"];
    [_scroller addSubview:_like];
    [_like release];
    
    
    self.likeCount = [[UILabel alloc] initWithFrame:CGRectZero];
    [_likeCount setFont:[UIFont systemFontOfSize:12]];
    //[_likeCount setBackgroundColor:[UIColor redColor]];
    [_scroller addSubview:_likeCount];
    [_likeCount release];
    
    self.one = [[UILabel alloc] initWithFrame:CGRectZero];
    [_one setFont:[UIFont systemFontOfSize:13]];
    [_one setText:@"人"];
    [_scroller addSubview:_one];
    
    self.comment = [[UILabel alloc] initWithFrame:CGRectZero];
    //[_comment setBackgroundColor:[UIColor redColor]];
    [_comment setText:@"评论"];
    [_scroller addSubview:_comment];
    [_comment release];
    
    
    self.commentCount = [[UILabel alloc] initWithFrame:CGRectZero];
    //[_commentCount setBackgroundColor:[UIColor redColor]];
    [_commentCount setFont:[UIFont systemFontOfSize:12]];
    [_scroller addSubview:_commentCount];
    [_commentCount release];
    
    self.two = [[UILabel alloc] initWithFrame:CGRectZero];
    [_two setFont:[UIFont systemFontOfSize:13]];
    [_two setText:@"人"];
    [_scroller addSubview:_two];

}

- (void)startAsync
{
    NSURL *url = [NSURL URLWithString:@"http://api.breadtrip.com/featured/photos/?start=0&count=12"];
    
    NSURLRequest *requsest = [NSURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:requsest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        NSMutableDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
        NSMutableArray *array = [dic objectForKey:@"items"];
        for (NSMutableDictionary *aDic in array) {
            picDetailModel *detail = [[picDetailModel alloc] init];
            [detail setValuesForKeysWithDictionary:aDic];
            [self.arr addObject:detail];
         
           
            [detail release];
        }
        picDetailModel *model = [_arr objectAtIndex:_num];
        NSURL *url1 = [NSURL URLWithString:[NSString stringWithFormat:@"http://api.breadtrip.com/trips/%ld/waypoints/%ld/replies/?count=32767",model.trip_id, model.id]];
       
        NSURLRequest *requsest1 = [NSURLRequest requestWithURL:url1];
        [NSURLConnection sendAsynchronousRequest:requsest1 queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
            NSMutableDictionary *dic2 = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
            NSNumber *num = [dic2 objectForKey:@"recommender_count"];
            NSNumber *num1 = [dic2 objectForKey:@"comment_count"];
            [ _likeCount setText:[NSString stringWithFormat:@"%@",num]];
            [_commentCount setText:[NSString stringWithFormat:@"%@", num1]];
            NSMutableArray *headArr = [dic2 objectForKey:@"recommenders"];
            NSMutableArray *comArray = [dic2 objectForKey:@"comments"];
            NSLog(@"%ld", [_likeCount.text integerValue]);
            if ( [_likeCount.text integerValue] <= 6) {
                for (int i = 0; i < [_likeCount.text integerValue];i ++) {
                    UIImageView *hedaView = [[UIImageView alloc] initWithFrame:CGRectMake(_like.frame.origin.x + i *40 , _like.frame.origin.y + _like.frame.size.height + 10, 35, 35)];
                    
                    [hedaView.layer setCornerRadius:17.5];
                    [hedaView setClipsToBounds:YES];
                    [hedaView setImageWithURL:[NSURL URLWithString:[[headArr objectAtIndex:i] objectForKey:@"avatar_m"]]];
                    [hedaView setBackgroundColor:[UIColor redColor]];
                    [_scroller addSubview:hedaView];
                    [hedaView release];
                }
            }
            
            if ( [_likeCount.text integerValue] > 6) {
                for (int i = 0; i < 4;i ++) {
                    UIImageView *hedaView = [[UIImageView alloc] initWithFrame:CGRectMake(_like.frame.origin.x + i * 40 , _like.frame.origin.y + _like.frame.size.height + 10, 35, 35)];
                    
                    [hedaView.layer setCornerRadius:17.5];
                    [hedaView setClipsToBounds:YES];
                    [hedaView setImageWithURL:[NSURL URLWithString:[[headArr objectAtIndex:i] objectForKey:@"avatar_m"]]];
                    [hedaView setBackgroundColor:[UIColor redColor]];
                    [_scroller addSubview:hedaView];
                    [hedaView release];
                }
            }
            
            [self.comment setFrame:CGRectMake(_like.frame.origin.x, _like.frame.origin.y + 75, 50, 25)];
            [self.commentCount setFrame:CGRectMake(_backImage.frame.size.width - 30, _like.frame.origin.y + 75, 25, 25)];
            [self.two setFrame:CGRectMake(_backImage.frame.size.width - 7, _like.frame.origin.y + 75, 25, 25)];
            [self createTableView];
            for (NSMutableDictionary *comDic in comArray) {
                commentModel *model = [[commentModel alloc] init];
                [model setValuesForKeysWithDictionary:comDic];
                [_array addObject:model];
                [_tableView reloadData];
                [model release];
            }
            [self.tableView setFrame:CGRectMake(0, _comment.frame.origin.y + _comment.frame.size.height + 10, self.view.frame.size.width, [_array count] * 50)];

            [_scroller setContentSize:CGSizeMake(0, _tableView.frame.size.height + _tableView.frame.origin.y + 70)];
        }];

      

        _model = [_arr objectAtIndex:_num];
        [_headImage setImageWithURL:[NSURL URLWithString: [_model.user objectForKey:@"avatar_m"]]];
        [_name setText:[_model.user objectForKey:@"name"]];
        [_trip_ads setText:_model.trip_name];
        [_backImage setImageWithURL:[NSURL URLWithString:_model.photo]];
        
        
        NSNumber *h = [_model.photo_info objectForKey:@"h"];
        NSNumber *w = [_model.photo_info objectForKey:@"w"];
        CGFloat h1 = [h floatValue];
        CGFloat w1 = [w floatValue];
        
        CGFloat height = self.view.frame.size.width * h1 / w1;
        [_backImage setFrame:CGRectMake(0, 0, self.view.frame.size.width - 30,height)];
        
        [_infor setText:_model.text];
        
        
        NSDictionary *dic1 = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:12],NSFontAttributeName, nil];
        CGRect rect = [_infor.text boundingRectWithSize:CGSizeMake(self.view.frame.size.width - 30, 300) options:NSStringDrawingUsesLineFragmentOrigin attributes:dic1 context:nil];
        [_infor setFrame:CGRectMake(_backImage.frame.origin.x + 5, _backImage.frame.origin.y + _backImage.frame.size.height, _backImage.frame.size.width - 10, rect.size.height)];
        
        [_time setText:_model.local_time];
        [_time setFrame:CGRectMake(_infor.frame.origin.x, _infor.frame.origin.y + _infor.frame.size.height, 150, 20)];
        
        [_smallImage setImageWithURL:[NSURL URLWithString:[_model.poi objectForKey:@"icon"]]];
        [_smallImage setFrame:CGRectMake(self.view.frame.size.width - 100, _time.frame.origin.y, 20, 20)];
        [_address setText:[_model.poi objectForKey:@"name"]];
        [_address setFrame:CGRectMake(_smallImage.frame.origin.x + _smallImage.frame.size.width + 5, _smallImage.frame.origin.y, 60, 20)];
       
        [_aView setFrame:CGRectMake(_headImage.frame.origin.x - 5, _headImage.frame.origin.y + _headImage.frame.size.height + 20, self.view.frame.size.width - 30, _time.frame.origin.y + _time.frame.size.height + 15)];
        [_like setFrame:CGRectMake(_aView.frame.origin.x, _aView.frame.size.height + _aView.frame.origin.y + 10, 50, 25)];
        
        [_likeCount setFrame:CGRectMake(_backImage.frame.size.width - 30, _like.frame.origin.y, 25, 25)];
        [_one setFrame:CGRectMake(_backImage.frame.size.width - 7, _like.frame.origin.y, 25, 25)];
       
        
    }];

}


@end
